package org.sean.generator.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sean.framework.bean.BaseResult;
import org.sean.framework.bean.PageData;
import org.sean.generator.dto.DataSource;
import org.sean.generator.dto.DataSourceQuery;
import org.sean.generator.manager.DataSourceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Coder Generator
 */
@RestController
@RequestMapping("/datasource")
@Tag(name = "DataSource", description = "")
public class DataSourceController {
    @Autowired
    private DataSourceManager dataSourceManager;

    @Operation(summary = "分页")
    @PostMapping("page")
    public BaseResult<PageData<DataSource>> page(@RequestBody DataSourceQuery query) {
        PageData<DataSource> result = dataSourceManager.page(query);
        return BaseResult.success(result);
    }

    @Operation(summary = "详情")
    @GetMapping("{id}")
    public BaseResult<DataSource> get(@PathVariable("id") Long id) {
        DataSource result = dataSourceManager.get(id);
        return BaseResult.success(result);
    }

    @Operation(summary = "保存")
    @PostMapping
    public BaseResult<Long> save(@RequestBody DataSource data) {
        dataSourceManager.save(data);
        return BaseResult.success(data.getId());
    }

    @Operation(summary = "修改")
    @PutMapping("{id}")
    public BaseResult<Void> update(@RequestBody DataSource data, @PathVariable("id") Long id) {
        data.setId(id);
        dataSourceManager.update(data);
        return BaseResult.success();
    }

    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public BaseResult<Void> delete(@PathVariable("id") Long id) {
        dataSourceManager.delete(id);
        return BaseResult.success();
    }

    @Operation(summary = "批量删除")
    @DeleteMapping
    public BaseResult<Void> delete(@RequestBody Long[] ids) {
        dataSourceManager.delete(ids);
        return BaseResult.success();
    }

}